require "AIData"
require "AIRoutine"

--[[
AI lib version 4
by Ivan[RUSSIA]

GPL v1 license

AICondition - lib namespace
	bool,float buffed(unit unit,string name) - also return buff's number
	bool manaLess(unit unit) - check energy/mana
	
	bool fastRecall(table pos) - check is faster run to spawn then recall at pos
	bool mid(table pos) - check is pos inside mid location
	bool top(table pos) - check is pos inside mid location
	bool bot(table pos) - check is pos inside mid location
	bool river(table pos) - check is pos inside classic map river
	bool enemySide(table pos) - check is pos inside enemy's side
	bool behind(table pos,table target) - check is pos behind target
	
	bool recall(unit unit) - check is recalling
	bool exhaust(unit unit) - check is unit exhausted
	bool ignite(unit unit) check is unit ignited
	bool immortal(unit unit) - check is unit immune to damage
	bool immune(unit unit) - check is unit immune to cc
--]]

AICondition = {}

AICondition.buffed = function(unit,name)
	for i = 1, unit.buffCount,1 do
		local buff = unit:getBuff(i)
		if buff.valid == true and string.find(buff.name,name) ~= nil then return true,i end
	end
	return false,0
end

AICondition.manaLess = function(unit)
	return (unit.maxMana <= 200 and unit.charName ~= "Vayne") or unit.charName == "Mordekaiser"
end

AICondition.fastRecall = function(pos)
	return AIRoutine.distance(AIData.allySpawn,pos) - 600 > myHero.ms * 8.5
end
	
AICondition.mid = function(pos)
	if AIData.map == "classic" then return GetDistance(pos,AIRoutine.project({x=1360,z=1630},{x=12570,z=12810},pos)) <= 750
	elseif AIData.map == "tt" then return GetDistance(pos,{x=7700,z=6700}) <= 1000
	elseif AIData.map == "dom" then return GetDistance(pos,{x=6900,z=6460}) <= 1000
	elseif AIData.map == "pg" then return true
	else return false end
end
	
AICondition.top = function(pos)
	if AIData.map == "classic" then return GetDistance(pos,{x=2000,z=12500}) <= 1000 or GetDistance(pos,AIRoutine.project({x=1000,z=1700},{x=1000,z=11500},pos)) <= 750 or GetDistance(pos,AIRoutine.project({x=12500,z=13250},{x=3400,z=13250},pos)) <= 750
	elseif AIData.map == "tt" then return GetDistance(pos,AIRoutine.project({x=2121,z=9000},{x=13285,z=9000},pos)) <= 1200
	else return false end
end
	
AICondition.bot = function(pos)
	if AIData.map == "classic" then return GetDistance(pos,{x=12100,z=2175}) <= 1000 or GetDistance(pos,AIRoutine.project({x=1330,z=1150},{x=12000,z=1150},pos)) <= 750 or GetDistance(pos,AIRoutine.project({x=13100,z=12850},{x=13100,z=3000},pos)) <= 750
	elseif AIData.map == "tt" then return GetDistance(pos,AIRoutine.project({x=2121,z=5600},{x=13285,z=5600},pos)) <= 850
	else return false end
end

AICondition.river = function(pos)
	return AIRoutine.distance(pos,AIRoutine.project({x=1000,z=13600},{x=13100,z=1500},pos)) <= 500
end

AICondition.enemySide = function(unit)
	return GetDistance(unit,AIData.enemySpawn) < GetDistance(unit,AIData.allySpawn)
end

AICondition.behind = function(unit,target)
	local rad = AIRoutine.rad(target,unit)
	--check for ally
	if target.team ~= ENEMY_TEAM then
		if AICondition.enemySide(unit) == false then return math.abs(AIRoutine.rad(unit,AIData.allySpawn) - rad) <= 0.785398163
		else return math.abs(AIRoutine.rad(AIData.enemySpawn,unit) - rad) <= 0.785398163 end
	else
		if AICondition.enemySide(unit) == true then return math.abs(AIRoutine.rad(unit,AIData.enemySpawn) - rad) <= 0.785398163
		else return math.abs(AIRoutine.rad(AIData.allySpawn,unit) - rad) <= 0.785398163 end
	end
end
	
AICondition.recall = function(unit) return AIData.particle(unit,"Teleport") and AICondition.buffed(unit,"Recall") == true end
AICondition.exhaust = function(unit) return AIData.particle(unit,"summoner_banish.troy") end
AICondition.ignite = function(unit) return AIData.particle(unit,"Summoner_Dot.troy") end

AICondition.immortal = function(unit)
	return unit.bInvulnerable == 1 or unit.bTargetable == false or AIData.particle(unit,"DiplomaticImmunity")
	or (unit.charName == "Tryndamere" and unit.health / unit.maxHealth <= 0.1 and AIData.particle(unit,"UndyingRage"))
end

AICondition.immune = function(unit)
	return AIData.particle(unit,"DiplomaticImmunity") or AIData.particle(unit,"olaf_ragnorok") or AIData.particle(unit,"Spellimmunity")
	or AIData.particle(unit,"SpellBlock") or AIData.particle(unit,"bansheesveil") or AIData.particle(unit,"shroudofdarkness")
end